package com.canbot.u05.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.Scene;

import java.util.List;

public class SceneChooseAdapter extends RecyclerView.Adapter<SceneChooseAdapter.MyViewHolder> {

    private static final String TAG = "AdminFaceGroupAdapter";
    private final TypedArray sceneDrawables;
    private Context mContext;
    private List<Scene> sceneList;
    private OnItemClickListener listener;
    private final String[] stringArray;
    private int chooseIndex = -1;

    public SceneChooseAdapter(Context context, List<Scene> sceneList) {
        this.mContext = context;
        this.sceneList = sceneList;
        stringArray = mContext.getResources().getStringArray(R.array.order_scene_names);
        sceneDrawables = mContext.getResources().obtainTypedArray(R.array.order_edit_module_drawables);
    }

    public void setItemClickListener(OnItemClickListener listener){

        this.listener = listener;
    }

    public void setSceneList(List<Scene> sceneList){
        this.sceneList = sceneList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_admin_scene_choose, viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
        String sceneName = sceneList.get(position).getSceneName();
        int drawableIndex = 0;
        for (int i = 0; i < stringArray.length; i++) {
            if (sceneName.equals(stringArray[i])){
                drawableIndex = i;
            }
        }
        myViewHolder.ivScene.setImageDrawable(mContext.getDrawable(sceneDrawables.getResourceId(drawableIndex,0)));
        myViewHolder.sceneName.setText(sceneName);
        if (position == chooseIndex){
            myViewHolder.ivSelected.setVisibility(View.VISIBLE);
            myViewHolder.sceneContainer.setBackgroundColor(Color.parseColor("#F3F3F3"));
        }else{
            myViewHolder.ivSelected.setVisibility(View.GONE);
            myViewHolder.sceneContainer.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        myViewHolder.sceneContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseIndex = position;
                notifyDataSetChanged();
                if (listener != null){
                    listener.onItemClick(sceneName);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return sceneList == null? 0 : sceneList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivSelected;
        private TextView sceneName;
        private ImageView ivScene;
        private RelativeLayout sceneContainer;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            sceneName = itemView.findViewById(R.id.tv_scene_name);
            sceneContainer = itemView.findViewById(R.id.rl_scene_container);
            ivScene = itemView.findViewById(R.id.iv_scene);
            ivSelected = itemView.findViewById(R.id.iv_scene_selected);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(String sceneName);
    }
}
